package Test;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import freemarker.WebTestListener;
@Listeners(WebTestListener.class)
public class After {	
	private WebDriver driver;

    @BeforeTest
    public void setup() {

        System.setProperty("webdriver.firefox.bin",
				"C:\\Program Files\\Mozilla Firefox\\firefox.exe");

        driver = new FirefoxDriver();
        driver.get("http://121.40.99.5/admin/admin/login");
    }

    @Test
    public void testLoginPage() throws InterruptedException {
    	//管理员后台登录
        String username = "admin";
        String password = "123456";
        String vertifyValue = "1234";

        WebElement usernameInput = driver.findElement(By.name("username"));
        WebElement passwordInput = driver.findElement(By.name("password"));
        WebElement vertifyInput = driver.findElement(By.id("vertify"));

        usernameInput.sendKeys(username);
        Thread.sleep(500);
        passwordInput.sendKeys(password);
        Thread.sleep(500);
        vertifyInput.sendKeys(vertifyValue);
        Thread.sleep(500);

        driver.findElement(By.name("submit")).click();
        Thread.sleep(1500);
        
        driver.findElement(By.xpath("/html/body/div[1]/div[3]/ul/li[12]/a")).click();
        Thread.sleep(1500);
    }
    public void MeterialManagePage() throws InterruptedException {
    	//素材管理模块
    	driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[6]/a")).click();
        Thread.sleep(1500);
        
      //管理员新增图文素材
        driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[1]/a")).click();
        Thread.sleep(1500);
        
        String title="123";
        String content="12345";
        WebElement titleInput = driver.findElement(By.name("title"));
        WebElement contentInput = driver.findElement(By.name("content"));
        titleInput.sendKeys(title);
        Thread.sleep(500);
        contentInput.sendKeys(content);
        Thread.sleep(500);		
        
        driver.findElement(By.cssSelector(".type-file-file")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("manage_tab")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/ul/li[2]/div[2]/img")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div/div/div[2]/ul/li[1]")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/form/div/div/a")).click();
        Thread.sleep(1500);
    	
      //管理员删除图文素材
        driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[2]/div/div[5]/div/span")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[5]/div[3]/a[1]")).click();
        Thread.sleep(1500);
        
      //管理员删除文本素材
        driver.findElement(By.xpath("/html/body/div[3]/div[1]/div/ul/li[2]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector(".trSelected > td:nth-child(5) > div:nth-child(1) > a:nth-child(2)")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[6]/div[3]/a[1]")).click();
        Thread.sleep(1500);
        
      //管理员编辑文本素材
        driver.findElement(By.xpath("/html/body/div[3]/div[1]/div/ul/li[2]/a/span")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(5) > div:nth-child(1) > a:nth-child(1)")).click();
        Thread.sleep(1500);
        
        WebElement titleToDelete = driver.findElement(By.name("title"));
        Actions actions = new Actions(driver);
        actions.moveToElement(titleToDelete).click().perform();
        actions.sendKeys(Keys.DELETE).build().perform();
        Thread.sleep(500);
        String title1="好多个";
        WebElement titleInput1 = driver.findElement(By.name("title"));
        titleInput1.sendKeys(title1);
        Thread.sleep(500);
        
        driver.findElement(By.xpath("/html/body/div[4]/form/div/div/a")).click();
        Thread.sleep(1500);
    }
    
    public void AddOrderPage() throws InterruptedException {
    	//微信菜单管理模块
        driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[2]/a")).click();
        Thread.sleep(1500);
        
      //管理员添加菜单
        driver.findElement(By.cssSelector("div.fbutton:nth-child(2) > a:nth-child(1) > div:nth-child(1) > span:nth-child(1)")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("tr.odd:nth-child(3) > td:nth-child(3) > div:nth-child(1) > select:nth-child(1) > option:nth-child(2)")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("div.fbutton:nth-child(3) > a:nth-child(1) > div:nth-child(1)")).click();
        Thread.sleep(1500);
    }
    
    public void OfficialAccountConfigurationPage() throws InterruptedException {
    	//微信公众号配置模块
        driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[1]/a")).click();
        Thread.sleep(1500);
        
      //管理员添加订单通知openid
        driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[1]/a")).click();
        Thread.sleep(1500);
        
        String bell_openid="123";
        WebElement bell_openidInput = driver.findElement(By.name("bell_openid"));
        bell_openidInput.sendKeys(bell_openid);
        Thread.sleep(500);
        
      //管理员更改微信接入状态
        driver.findElement(By.cssSelector("dl.row:nth-child(12) > dd:nth-child(2) > input:nth-child(2)")).click();
        Thread.sleep(1500);
        
      //管理员更改微信号
        WebElement weixinToDelete = driver.findElement(By.name("weixin"));
        Actions actions = new Actions(driver);
        actions.moveToElement(weixinToDelete).click().perform();
        actions.sendKeys(Keys.DELETE).build().perform();
        Thread.sleep(500);
        String weixin="TPshop";
        WebElement nicknameInput = driver.findElement(By.name("weixin"));
        nicknameInput.sendKeys(weixin);
        Thread.sleep(500);
        
      //管理员更改微信号类型
        driver.findElement(By.cssSelector(".small > option:nth-child(3)")).click();
        Thread.sleep(1500);
        
      //管理员选择上传头像二维码
        driver.findElement(By.cssSelector("dl.row:nth-child(6) > dd:nth-child(2) > div:nth-child(1) > span:nth-child(2) > input:nth-child(3)")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("manage_tab")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[1]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("li.file:nth-child(3) > div:nth-child(2) > span:nth-child(2)")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div/div/div[2]/ul/li[1]")).click();
        Thread.sleep(1500);
        
        driver.findElement(By.id("submitBtn")).click();
        Thread.sleep(1500);    	
    }
    
    public void TemplateMessagePage() throws InterruptedException {
    	//模板消息模块
        driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[5]/a")).click();
        Thread.sleep(1500);
        
      //管理员添加模板消息备注
        driver.findElement(By.cssSelector(".trSelected > td:nth-child(6) > div:nth-child(1) > a:nth-child(1)")).click();
        Thread.sleep(1500);
        
        String text="123";
        WebElement textInput = driver.findElement(By.name("text"));
        textInput.sendKeys(text);
        Thread.sleep(500);
        
        driver.findElement(By.xpath("/html/body/div[4]/div[2]/div/div/a")).click();
        Thread.sleep(1500);
        
      //管理员重置消息模板
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(6) > div:nth-child(1) > a:nth-child(2)")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(6) > div:nth-child(1) > a:nth-child(2)")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(6) > div:nth-child(1) > a:nth-child(2)")).click();
        Thread.sleep(1500);
        
      //管理员启用消息模板
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(5) > div:nth-child(1) > span:nth-child(1) > i:nth-child(1)")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(5) > div:nth-child(1) > span:nth-child(1) > i:nth-child(1)")).click();
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("#flexigrid > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(5) > div:nth-child(1) > span:nth-child(1) > i:nth-child(1)")).click();
        Thread.sleep(1500);
    }
    
    public void AutomaticReplyPage() throws InterruptedException {
    	//自动回复模块
    	driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[14]/dl/dd/ul/li[3]/a")).click();
        Thread.sleep(1500);
        
      //管理员删除自动回复
        driver.findElement(By.cssSelector("th.sign > div:nth-child(1) > i:nth-child(1)")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[1]/div[3]/a/div/span")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[6]/div[3]/a[1]")).click();
        Thread.sleep(1500);
        
      //管理员编辑自动回复内容
        driver.findElement(By.cssSelector("#tbody > tr:nth-child(1) > td:nth-child(6) > div:nth-child(1) > a:nth-child(1)")).click();
        Thread.sleep(1500);
        
        WebElement ruleToDelete = driver.findElement(By.name("rule"));
        Actions actions = new Actions(driver);
        actions.moveToElement(ruleToDelete).click().perform();
        actions.sendKeys(Keys.DELETE).build().perform();
        Thread.sleep(500);
        String rule="退款";
        WebElement ruleInput = driver.findElement(By.name("rule"));
        ruleInput.sendKeys(rule);
        Thread.sleep(500);
        
        driver.findElement(By.xpath("/html/body/div[4]/form/div/div/a")).click();
        Thread.sleep(1500);
    }
   @AfterTest
    public void teardown() {
   	if (driver != null) {
        driver.quit();
    	}
    }
}